package com.gangzi.liteormbyrxjava;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 參考博客：http://gank.io/post/560e15be2dca930e00da1083
 * https://github.com/litesuits/android-lite-orm
 */

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";
    ArrayList<StudentBean> arrayList;
    private TextView tv_share;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_share= (TextView) findViewById(R.id.tv_share);
        tv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "test share to send.");
                sendIntent.setType("text/plain");
                startActivity(Intent.createChooser(sendIntent, "分享到："));
            }
        });
//        addLiteData();
//        queryLiteData();
//        queryDataByClass(StudentBean.class);
//        findData();
    }

    private void findData() {
        MyApplication.getLiteOrm().delete(TeacherBean.class);
        TeacherBean teacherOne=new TeacherBean();
        MyApplication.getLiteOrm().save(teacherOne);
        TeacherBean teacherTwo=new TeacherBean();
        teacherTwo.setName("two");
        teacherTwo.setAge(10);
        MyApplication.getLiteOrm().save(teacherTwo);
        ArrayList<TeacherBean> list=MyApplication.getLiteOrm().query(TeacherBean.class);
        for(TeacherBean ele:list){
            Log.i(TAG,"ele:"+ele.getClass_name()+" sex:"+ele.isSex());
        }

        //相等查询
        ArrayList<TeacherBean> listTwo=MyApplication.getLiteOrm().query(new QueryBuilder<>(TeacherBean.class)
                .whereEquals(TeacherBean.AGE,10));
        for(TeacherBean ele:listTwo){
            Log.i(TAG,"listTwo ele:"+ele.toString());
        }

        MyApplication.getLiteOrm().delete(TeacherBean.class);
        TeacherBean teacherThree=new TeacherBean();
        teacherThree.setName("three");
        teacherThree.setAge(10);
        MyApplication.getLiteOrm().insert(teacherThree);

        ArrayList<TeacherBean> listThree=MyApplication.getLiteOrm().query(TeacherBean.class);
        for(TeacherBean ele:listThree){
            Log.i(TAG,"listThree ele:"+ele.toString());
            ele.setName(ele.getName()+"123");
            MyApplication.getLiteOrm().save(ele);
        }
        for(TeacherBean ele:listThree){
            Log.i(TAG,"update listThree ele:"+ele.toString());
        }
    }

    /**
     *
     * IO线程中操作数据库操作，结果在主线程中返回
     * @param object
     */
    private void queryDataByClass(Object object) {
        Observable.just(object)
                .subscribeOn(Schedulers.io())  //在IO线程发起
                .observeOn(Schedulers.io())  //管下
                .map(new Func1<Object, ArrayList>() {  //操作符
                    @Override
                    public ArrayList call(Object object) {
                        //执行
                        Log.d(TAG,"执行Thread："+Thread.currentThread().getName());
                        return MyApplication.getLiteOrm().query((Class)object);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList>() {
                    @Override
                    public void call(ArrayList list) {
                        Log.d(TAG,"结果Thread："+Thread.currentThread().getName());
                        arrayList =list;
                        for(StudentBean ele:arrayList){
                            Log.d(TAG,"for:"+ele.toString());
                        }
                    }
                });
    }

    private void queryLiteData() {
        Log.d(TAG,"before query time:"+System.currentTimeMillis());
        ArrayList<StudentBean> arrayList= MyApplication.getLiteOrm().query(StudentBean.class);
        Log.d(TAG,"after query time:"+System.currentTimeMillis());

//        for(StudentBean ele:arrayList){
//            Log.d(TAG,ele.toString());
//        }
        Log.d(TAG,"size:"+arrayList.size());
    }

    private void addLiteData() {
        MyApplication.getLiteOrm().delete(StudentBean.class);

        Log.d(TAG,"before insert time:"+System.currentTimeMillis());
        for(int i=0;i<10000;i++){
            StudentBean studentBean=new StudentBean(""+i,true);
            MyApplication.getLiteOrm().insert(studentBean);
        }
        Log.d(TAG,"after insert time:"+System.currentTimeMillis());
    }
}
